package vip.xiaonuo.sys.fymodule.account.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class CommonUtil {
    // 预编译正则表达式提升性能（包含常见特殊字符）
    private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile("[@#%&*()+=\\-|<>?{}\\[\\]~`$^\\\\]");

    private static final SecureRandom random = new SecureRandom();
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmssSSS");

    public static boolean containsSpecialChars(String input) {
        if (input == null || input.isEmpty()) {
            return false;
        }
        return SPECIAL_CHAR_PATTERN.matcher(input).find();
    }

    public static String getYyyyMMdd(String input) {
        long timestamp = Long.parseLong(input);
        DateTime dateTime = DateUtil.date(timestamp * 1000);
        return DateUtil.format(dateTime, "yyyyMMdd");
    }

    public static String createCode(String prefix) {
        String time = DATE_TIME_FORMATTER.format(LocalDateTime.now());
        String s = time + (int) (random.nextDouble() * 100);
        return prefix + s;
    }

    /**
     * 获取本机IPv4地址（非回环地址）
     */
    public static String getLocalIpAddress() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // 过滤回环接口和未启用的接口
                if (iface.isLoopback() || !iface.isUp()) continue;

                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    // 只返回IPv4地址
                    if (addr instanceof Inet4Address) {
                        return addr.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取本机MAC地址
     */
    public static String getLocalMacAddress() {
        try {
            NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
            if (network == null) {
                return "";
            }
            byte[] mac = network.getHardwareAddress();
            if (mac == null) {
                return "";
            }
            // 将字节数组转换为可读格式
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
